package com.youyoulong.framework.util;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/** 
 * 此类的描述：  用于处理线程相关的变量，如当前的REQUEST
 * @author:  Administrator
 * @最后修改人： Administrator 
 * @最后修改日期:2015年12月17日 下午9:48:16
 * @version: 2.0
 */ 
    
public class ThreadVariableUtil
{
    public final static String _currentRequestKey="currentRequest";
    public static final String _sessionUserInfoKey = "currentUser";
    public static final String _sessionLocaleKey = "locale";
    public static final String _sessionLanguageKey = "language";
    
    private ThreadVariableUtil(){};
    
    private static class Handler{
       static  ThreadVariableUtil handler=new ThreadVariableUtil();
    }
    
    @SuppressWarnings("rawtypes")
    private ThreadLocal<Map> threadContext = new ThreadLocal<Map>(){
        protected Map initialValue()
        {
            return new HashMap(10,1);
        }
    };

    public static ThreadVariableUtil create(){
        return Handler.handler;
    }
    
   
    @SuppressWarnings("unchecked")
    public void setAttribute(String key,Object value){
        threadContext.get().put(key, value);
    }
    
    public Object getAttribute(String key){
        return threadContext.get().get(key);
    }
    
    public Locale getCurrentLocale(){
    	HttpServletRequest request = (HttpServletRequest)ThreadVariableUtil.create().getAttribute(_currentRequestKey);
    	return (Locale)request.getSession().getAttribute(_sessionLanguageKey);
    }
    
}
